package com.hemi.rhet;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.hemi.rhet.util.AppParam;
import com.hemi.rhet.util.FileUtil;

public class PhotoUpload extends Activity {
	Bundle savedInstanceState = null;
	private String imageInfo;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.savedInstanceState = savedInstanceState;
		setContentView(R.layout.photo_upload);
	}

	@Override
	public void startActivityForResult(Intent intent, int requestCode) {
		// TODO Auto-generated method stub
		super.startActivityForResult(intent, requestCode);
		intent.putExtra("imageInfo", imageInfo);
	}

	@Override
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
		openOptionsMenu();
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (TAKE_PHOTO_ID == requestCode) {
			if (resultCode == RESULT_OK && filePath!=null){
				data = new Intent();
				data.putExtra("photoPath", filePath);
				 AppParam pdatabase=  AppParam.newInstance(PhotoUpload.this);;
				 String reates =   pdatabase.get("carema.rsize");
				 int reate =0;
				 if(reates!=null)
				 {
					 reate=Integer.parseInt(reates);
				 }
	  			try {
					FileUtil.getImageBitmapAndSetRotate(filePath, reate);
				} catch (IOException e) {
					e.printStackTrace();
				}
				setResult(RESULT_OK, data);
			}
			finish();
		}else if (FROM_ALARM == requestCode) {
			if(data!=null && data.getData()!=null)
			{
			    ContentResolver resolver = getContentResolver();
			        try {
			            Uri originalUri = data.getData();  
			            Bitmap bm = MediaStore.Images.Media.getBitmap(resolver, originalUri);        
			            String[] proj = {MediaStore.Images.Media.DATA};
			            Cursor cursor = managedQuery(originalUri, proj, null, null, null); 
			            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
			            cursor.moveToFirst();
			            filePath = cursor.getString(column_index);
			            data.putExtra("photoPath", filePath);
						setResult(RESULT_OK, data);
						filePath =null;
						
			        }catch (IOException e) {
			        	
			        }
			}
			finish();
		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, TAKE_PHOTO_ID, 0, R.string.take_photo);
		// menu.add(0, UPLOAD_PHOTO_ID,0, R.string.upload_photo);
		 menu.add(0,FROM_ALARM,0, R.string.from_alarm);
		menu.add(0, BACK_ID, 0, R.string.back_button);
		return true;
	}

	String filePath ;
	// @Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		boolean result = true;
		switch (item.getItemId()) {
		case TAKE_PHOTO_ID:
			Log.i("info", "ready to take photos!");
			Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
			filePath = ParamUtil.getDatedFileName(Constants.SD_CARD_TEMP_DIR);
			i.putExtra(MediaStore.EXTRA_OUTPUT,
					Uri.fromFile(new File(filePath)));
			startActivityForResult(i, TAKE_PHOTO_ID);
			result = true;
			break;
		case UPLOAD_PHOTO_ID:
			break;
		case FROM_ALARM:
			// Intent g= new Intent("android.media.action.IMAGE_CAPTURE");
			// startActivityForResult(g, FROM_ALARM);

			Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
			intent.addCategory(Intent.CATEGORY_OPENABLE);
			
			
			
			intent.setType("image/*");
//			intent.putExtra("crop", "true");
//			intent.putExtra("aspectX", 1);
//			intent.putExtra("aspectY", 1);
//			intent.putExtra("outputX", 80);
//			intent.putExtra("outputY", 80);
			intent.putExtra("return-data", true);

			startActivityForResult(intent, FROM_ALARM);

			result = true;
			break;
		case BACK_ID:
			this.finish();
			break;
		default:
			result = super.onMenuItemSelected(featureId, item);
			break;
		}
		return result;
	}

 

	// =============================================
	// private Bitmap photoCaptured;
	private static final int TAKE_PHOTO_ID = Menu.FIRST;
	private static final int UPLOAD_PHOTO_ID = Menu.FIRST + 1;
	private static final int BACK_ID = Menu.FIRST + 3;
	private static final int FROM_ALARM = Menu.FIRST + 5;



}
